/*
 * jxSwing Project (2012, 2013, 2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.text;

import java.awt.Dimension;

import javax.swing.InputVerifier;
import javax.swing.JComponent;

import org.jxSwing.i18n.I18N;
import org.jxUtils.system.NetworkUtil;

/**
 * @author Denilson Edinaldo Pinto
 * @project jxSwing
 * @package org.jxSwing.component.text
 * @date 14/09/2012 - 20:01:24
 */
public final class IPv4Field extends TextField {
	
	private static final long serialVersionUID = -4191259694815171818L;
	
	private static final int columns = 15;
	
	/**
	 * 
	 */
	public IPv4Field() {
	
		this(false);
	}
	
	/**
	 * @param acceptDecimal
	 */
	public IPv4Field(final boolean required) {
	
		super(columns, required);
		
		setName(I18N.component().ipv4());
		setSize(new Dimension(80, 25));
		setPreferredSize(new Dimension(80, 25));
		setHorizontalAlignment(CENTER);
		
		//
		setInputVerifier(new InputVerifier() {
			
			@Override
			public boolean verify(final JComponent input) {
			
				return isEmpty() || NetworkUtil.isIPv4(getText());
			}
		});
	}
	
	/**
	 * @return {@code getText().trim().isEmpty()}
	 */
	public boolean isEmpty() {
	
		return getText().trim().isEmpty();
	}
}
